﻿//======================================================
//==     (c)2008 aspxcms inc by NeTCMS v1.0              ==
//==          Forum:bbs.aspxcms.com                   ==
//==         Website:www.aspxcms.com                  ==
//======================================================
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NetCMS.Content;
using NetCMS.Model;

public partial class manage_channel_Content : NetCMS.Web.UI.ManagePage
{
    public static string itemname = string.Empty;
    public static string DataTable = string.Empty;
    public string OpStr = string.Empty;
    public string addStr = string.Empty;
    public string updateStr = string.Empty;
    public string delStr = string.Empty;
    public string OneStr = string.Empty;
    public string lockStr = string.Empty;
    public string ResetStr = string.Empty;
    public string spStr = string.Empty;
    public string xmlStr = string.Empty;
    public string htmStr = string.Empty;

    Channel rd = new Channel();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Authority_Code = "D003";
        this.CheckAdminAuthority();
        addStr = checkPr("D002", "0");
        updateStr = checkPr("D015", "0");

        delStr = checkPr("D016", "0");
        OneStr = checkPr("D017", "0");
        lockStr = checkPr("D018", "0");
        ResetStr = checkPr("D019", "0");
        spStr = checkPr("D020", "0");
        xmlStr = checkPr("D021", "0");
        htmStr = checkPr("D022", "0");




        Response.CacheControl = "no-cache";
        this.PageNavigator1.OnPageChange += new PageChangeHandler(PageNavigator1_PageChange);
        if (!IsPostBack)
        {
            if (SiteID != "0")
            {
                PageError("分站没权限操作", "javascript:history.back();", true);
            }
            string ChID = Request.QueryString["ChID"];
            string ClassID = Request.QueryString["ClassID"];
            int sClassID = 0;
            if (ChID != string.Empty && ChID != null)
            {
                DataTable = rd.getChannelTable(int.Parse(ChID.ToString()));
                if (DataTable == "#")
                {
                    PageError("找不到数据库表，发生异常！", "javascript:history.back();", true);
                }
                //判断是否允许添加信息
                if (rd.getChannelParentCount(int.Parse(ChID)) > 0)
                {
                    PageError("有子类的频道不允许添加信息。<li><span style=\"color:red;\">请选择子类添加信息！</span></li>", "javascript:history.back();", false);
                }

                string alock = Request.QueryString["alock"];
                if (alock != string.Empty && alock != null)
                {
                    string id = Request.QueryString["id"];
                    switch (alock.ToString())
                    {
                        case "0":
                            rd.lockContent(int.Parse(id.ToString()),int.Parse(ChID.ToString()),0);
                            PageRight("解锁成功！", "javascript:history.back();", true);
                            break;
                        case "1":
                            rd.lockContent(int.Parse(id.ToString()), int.Parse(ChID.ToString()), 1);
                            PageRight("锁定成功！", "javascript:history.back();", true);
                            break;
                        case "order":
                            int orderID = 0;
                            if (NetCMS.Common.Input.IsInteger(Request.QueryString["OrderId"]))
                            {
                                orderID = int.Parse(Request.QueryString["OrderId"]);
                            }
                            rd.setOrderContent(int.Parse(id.ToString()), int.Parse(ChID.ToString()), orderID);
                            PageRight("设置权重为[" + orderID + "]成功！", "javascript:history.back();", true);
                            break;
                    }
                }
                string iclassID = "#0";
                string stat = "#0";
                string specialID = "#0";
                string islock = "#0";
                string author = "#0";
                if (ClassID != string.Empty && ClassID != null)
                {
                    sClassID = int.Parse(ClassID.ToString());
                    iclassID = ClassID.ToString();
                    cClassName.InnerHtml = rd.getClassName(int.Parse(ClassID.ToString()));
                }
                else
                {
                    cClassName.InnerHtml = "所有栏目";
                    classAll.Visible = false;
                }
                if (Request.QueryString["stat"] != null && Request.QueryString["stat"] != string.Empty)
                {
                    stat = Request.QueryString["stat"];
                }
                if (Request.QueryString["SpecialID"] != null && Request.QueryString["SpecialID"] != string.Empty)
                {
                    specialID = Request.QueryString["SpecialID"];
                }
                if (Request.QueryString["islock"] != null && Request.QueryString["islock"] != string.Empty)
                {
                    islock = Request.QueryString["islock"];
                }
                if (Request.QueryString["author"] != null && Request.QueryString["author"] != string.Empty)
                {
                    author = Request.QueryString["author"];
                }
                copyright.InnerHtml = CopyRight;
                GetClassList((this.gClassID), 0, 0, sClassID);
                IDataReader dr = rd.getModelinfo(int.Parse(ChID.ToString()));
                if (dr.Read())
                {
                    itemname = dr["channelItem"].ToString();
                }
                dr.Close();
                StartLoad(1, "#0", islock, author, iclassID, specialID, stat);
            }
            else
            {
                PageError("错误的频道参数", "javascript:history.back();", true);
            }
        }
    }

    //分页处理
    protected void PageNavigator1_PageChange(object sender, int PageIndex)
    {
        string iclassID = "#0";
        string stat = "#0";
        string specialID = "#0";
        string islock = "#0";
        string author = "#0";
        if (ClassID != string.Empty && ClassID != null)
        {
            iclassID = ClassID.ToString();
        }
        if (Request.QueryString["stat"] != null && Request.QueryString["stat"] != string.Empty)
        {
            stat = Request.QueryString["stat"];
        }
        if (Request.QueryString["SpecialID"] != null && Request.QueryString["SpecialID"] != string.Empty)
        {
            specialID = Request.QueryString["SpecialID"];
        }
        if (Request.QueryString["islock"] != null && Request.QueryString["islock"] != string.Empty)
        {
            islock = Request.QueryString["islock"];
        }
        if (Request.QueryString["author"] != null && Request.QueryString["author"] != string.Empty)
        {
            author = Request.QueryString["author"];
        }
        StartLoad(PageIndex, "#0", islock, author, iclassID, specialID, stat);
    }

    //数据初始化
    protected void StartLoad(int PageIndex,string keywords,string islock,string author,string ClassID,string specialid,string stat)
    {
        int i, j;
        string ChID = Request.QueryString["ChID"];
        string gkeywords = "#0";
        if (keywords != "#0")
        {
            gkeywords = keywords;
        }
        string gislock = "#0";
        if (islock != "#0")
        {
            gislock = islock;
        }

        string gauthor = "#0";
        if (author != "#0")
        {
            gauthor = author;
        }

        string gClassID = "#0";
        if (ClassID != "#0")
        {
            gClassID = ClassID;
        }

        string gspecialid = "#0";
        if (specialid != "#0")
        {
            gspecialid = specialid;
        }

        string gstat = "#0";
        if (stat != "#0")
        {
            gstat = stat;
        }

        DataTable dt = rd.GetPage(gkeywords, gislock, gauthor, gClassID, gspecialid, gstat, int.Parse(Request.QueryString["ChID"]), DataTable, PageIndex, 50, out i, out j, null);
        this.PageNavigator1.PageCount = j;
        this.PageNavigator1.PageIndex = PageIndex;
        this.PageNavigator1.RecordCount = i;
        string strchar = string.Empty;
        string PoPstr = string.Empty;
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Columns.Add("CName", typeof(string));
            dt.Columns.Add("islocks", typeof(string));
            dt.Columns.Add("ClassName", typeof(string));
            dt.Columns.Add("gauthor", typeof(string));
            dt.Columns.Add("op", typeof(string));
            for(int k=0;k<dt.Rows.Count;k++)
            {
                dt.Rows[k]["ClassName"] = "<a href=\"Content.aspx?ChID=" + ChID.ToString() + "&ClassID=" + dt.Rows[k]["ClassID"].ToString() + "\" class=\"list_link\">"+rd.getClassName(int.Parse(dt.Rows[k]["classID"].ToString()))+"</a>";
                dt.Rows[k]["CName"] = "<a class=\"list_link\" href=\"Content_add.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&Action=edit\" title=\"点击修改\">" + dt.Rows[k]["title"].ToString() + "</a><span class=\"reshow\" title=\"点击次数\">(" + dt.Rows[k]["click"].ToString() + ")</span>";

                if (updateStr != string.Empty)
                {
                    dt.Rows[k]["CName"] = "<a class=\"list_link\" href=\"Content_add.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&Action=edit\" title=\"点击修改\">" + dt.Rows[k]["title"].ToString() + "</a><span class=\"reshow\" title=\"点击次数\">(" + dt.Rows[k]["click"].ToString() + ")</span>";
                    dt.Rows[k]["op"] = "<a href=\"Content_add.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&Action=edit\" title=\"点击修改\"><img src=\"../../sysImages/" + NetCMS.Config.UIConfig.CssPath() + "/sysico/edit.gif\" border=\"0\"></a>";
                }
                else
                {
                    dt.Rows[k]["CName"] = dt.Rows[k]["title"].ToString()+"<span class=\"reshow\" title=\"点击次数\">(" + dt.Rows[k]["click"].ToString() + ")</span>";
                }
                
                dt.Rows[k]["op"] += "<a href=\"preview.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&type=content\"  target=\"_blank\"><img src=\"../../sysImages/" + NetCMS.Config.UIConfig.CssPath() + "/sysico/review.gif\" border=\"0\"></a> <input type=\"checkbox\" name=\"infoID\" value=\"" + dt.Rows[k]["id"].ToString() + "\">";
                if (dt.Rows[k]["islock"].ToString() == "1")
                {
                    dt.Rows[k]["islocks"] = "<a href=\"Content.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&alock=0\" title=\"点击解锁\" class=\"reshow\" OnClick=\"{if(confirm('确定要解锁吗？')){return true;}return false;}\">锁定</a>";
                }
                else
                {
                    dt.Rows[k]["islocks"] = "<a href=\"Content.aspx?ChID=" + ChID.ToString() + "&id=" + dt.Rows[k]["id"].ToString() + "&alock=1\" title=\"点击锁定\" class=\"list_link\"  OnClick=\"{if(confirm('确定要锁定吗？')){return true;}return false;}\">正常</a>";
                }
                if (dt.Rows[k]["isconstr"].ToString() == "1")
                {
                    dt.Rows[k]["gauthor"] = "<a class=\"list_link\" href=\"Content.aspx?ChID=" + ChID.ToString() + "&ClassID=" + dt.Rows[k]["ClassID"].ToString() + "&author=" + dt.Rows[k]["author"].ToString() + "\" title=\"此信息为前台会员录入\">" + dt.Rows[k]["author"].ToString() + "</a> (<a href=\"../../" + NetCMS.Config.UIConfig.dirUser + "/showuser-" + dt.Rows[k]["author"].ToString() + ".aspx\" target=\"_blank\" title=\"查看此会员的资料\"  class=\"reshow\">查看</a>)";
                }
                else
                {
                    dt.Rows[k]["gauthor"] = "<a class=\"list_link\" href=\"Content.aspx?ChID=" + ChID.ToString() + "&ClassID=" + dt.Rows[k]["ClassID"].ToString() + "&author=" + dt.Rows[k]["author"].ToString() + "\" title=\"管理员录入\">" + dt.Rows[k]["author"].ToString() + "</a>";
                }
            }
        }
        DataList1.DataSource = dt;
        DataList1.DataBind();
        dt.Clear(); dt.Dispose();
    }

    /// <summary>
    /// 获取栏目列表
    /// </summary>
    /// <param name="lst"></param>
    /// <param name="ParentID"></param>
    /// <param name="Layer"></param>
    /// <param name="sClassID"></param>
    protected void GetClassList(DropDownList lst, int ParentID, int Layer, int sClassID)
    {
        IDataReader dr = rd.getClassList(ParentID, int.Parse(Request.QueryString["ChID"]));
        while (dr.Read())
        {
            ListItem it = new ListItem();
            string stxt = "";
            it.Value = dr["ID"].ToString();
            if (Layer > 0)
                stxt = "┝";
            for (int i = 1; i < Layer; i++)
            {
                stxt += " ┉ ";
            }
            it.Text = stxt + dr["ClassCName"].ToString();
            if (sClassID == int.Parse(dr["id"].ToString()))
            {
                it.Selected = true;
            }
            lst.Items.Add(it);
            GetClassList(lst, int.Parse(dr["ID"].ToString()), (Layer + 1), sClassID);
        }
        dr.Close();
    }

    protected void delContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "javascript:history.back();",true);
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.delContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]),0) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功删除" + j + "个信息!,失败" + m + "个信息。", "javascript:history.back();", true);
        }
    }

    protected void Reset_allContent(object sender, EventArgs e)
    {
        int cid = 0;
        if (Request.QueryString["ClassID"] != null && Request.QueryString["ClassID"] != string.Empty)
        {
            cid = int.Parse(Request.QueryString["ClassID"]);
        }
        int DelNum = rd.delContent(cid, int.Parse(Request.QueryString["ChID"]), 1);
        PageRight("成功删除" + DelNum + "个信息!", "javascript:history.back();", true);
    }

    protected void Lock_allContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.lockContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 1) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功锁定" + j + "个信息!,失败" + m + "个信息。", "javascript:history.back();", true);
        }
    }

    protected void UnLock_allContent(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.lockContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 0) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功解锁" + j + "个信息!,失败" + m + "个信息。", "javascript:history.back();", true);
        }
    }

    protected void Makexml(object sender, EventArgs e)
    {
        string ClassID = Request.QueryString["ClassID"];
        int GClassID = 0;
        if (ClassID != null && ClassID != string.Empty)
        {
            GClassID = int.Parse(ClassID.ToString());
        }
        int ChID = int.Parse(Request.QueryString["ChID"]);
        NetCMS.Control.HProgressBar.Start();
        try
        {
            NetCMS.Control.HProgressBar.Roll("正在发布XML...", 0);
            if (NetCMS.Publish.General.publishCHXML(GClassID, ChID))
            NetCMS.Control.HProgressBar.Roll("正在发布...", 100);
            NetCMS.Control.HProgressBar.Roll("发布xml成功&nbsp;<a class=\"list_link\" href=\"javascript:history.back();\">返回</a>", 100);
        }
        catch (Exception ex)
        {
            NetCMS.Common.Public.savePublicLogFiles("□□□发布XML，频道ID" + Request.QueryString["ChID"] + "", "【错误描述：】\r\n" + ex.ToString(), UserName);
            NetCMS.Control.HProgressBar.Roll("发布XML失败。<a class=\"list_link\" href=\"../Pulish/error/geterror.aspx?\">查看日志</a>", 0);
        }
        Response.End();
    }

    protected void MakeHtml(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        int ChID=int.Parse(Request.QueryString["ChID"]);
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            NetCMS.Control.HProgressBar.Start();
            try
            {
                NetCMS.Control.HProgressBar.Roll("正在发布频道信息", 0);
                string[] Checkboxs = Str.Split(',');
                Str = null;
                int j = 0;
                int m = Checkboxs.Length;
                for (int i = 0; i < m; i++)
                {

                    if (NetCMS.Publish.General.publishCHSingleNews(int.Parse(Checkboxs[i].ToString()), rd.getClassIDfromTable(int.Parse(Checkboxs[i].ToString()), ChID), ChID))
                    {
                        j++;
                    }
                   NetCMS.Control.HProgressBar.Roll("共生成" + m + "个信息，正在发布" + (i + 1) + "个。", ((i + 1) * 100 / m));
                }
                NetCMS.Control.HProgressBar.Roll("发布信息成功,成功" + j + "个,<a class=\"list_link\" href=\"../Publish/error/geterror.aspx?\">失败" + (Checkboxs.Length - j) + "个(可能信息有浏览权限)</a>. &nbsp;<a href=\"javascript:history.back();\">返回</a>", 100);
            }
            catch (Exception ex)
            {
                NetCMS.Common.Public.savePublicLogFiles("□□□发布新闻，频道ID" + ChID.ToString() + "", "【错误描述：】\r\n" + ex.ToString(), UserName);
                NetCMS.Control.HProgressBar.Roll("发布信息失败。<a class=\"list_link\" href=\"../Pulish/error/geterror.aspx?\">查看日志</a>", 0);
            }
            Response.End();
        }
    }

    protected void Reset_OrderID(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            string[] gIDARR = Str.Split(',');
            Str = null;
            int j = 0;
            int m = 0;
            for (int i = 0; i < gIDARR.Length; i++)
            {
                if (rd.lockContent(int.Parse(gIDARR[i]), int.Parse(Request.QueryString["ChID"]), 2) != 0)
                {
                    j++;
                }
                else
                {
                    m++;
                }
            }
            PageRight("成功重置权重" + j + "个信息!,失败" + m + "个信息。", "javascript:history.back();", true);
        }
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string gkeywords = this.keywords.Text;
            string iclassID = "#0";
            if (Request.QueryString["ClassID"] != string.Empty && Request.QueryString["ClassID"] != null)
            {
                iclassID = Request.QueryString["ClassID"];
            }
            if (gkeywords != string.Empty & gkeywords != null)
            {
                StartLoad(1, gkeywords, "#0", "#0", iclassID, "#0", "#0");
            }
        }
    }

    protected void ToSpecial(object sender, EventArgs e)
    {
        string Str = Request.Form["infoID"];
        if (Str == null || Str == String.Empty)
        {
            PageError("请至少选择一项!", "");
        }
        else
        {
            Response.Redirect("ContentToSpecial.aspx?id=" + NetCMS.Common.Input.URLEncode(Str) + "&ChID=" + Request.QueryString["ChID"] + "");
      }
    }

    /// <summary>
    /// 权限判断
    /// </summary>
    /// <param name="code">权限代码</param>
    /// <returns></returns>
    public string checkPr(string code, string tempStr)
    {
        this.Authority_Code = code;
        string str = string.Empty;
        if (CheckAuthority())
        {
            str = tempStr;
        }
        return str;
    }
}
